package top.mengchaob.deploy.controller;

import top.mengchaob.deploy.api.HostServiceI;
import top.mengchaob.deploy.table.HostDo;
import top.mengchaob.util.UtilTimeFormatter;
import top.mengchaob.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @title：
 * @author: mengchaob
 * @date: 2021年04月28日 23:26
 * @description:
 */
@Controller
@RequestMapping("hostController")
public class HostController {
    @Autowired
    HostServiceI hostServiceI;
    @RequestMapping("addHost")
    @ResponseBody
    public ResultBean addHost(@RequestBody HostDo hostDo, HttpServletRequest request){
        ResultBean rs = null;
        try {
            hostDo.setCreateTime(UtilTimeFormatter.getTimeToYMDHMS(new Date().getTime()));
            hostDo.setId(UUID.randomUUID().toString());
            hostServiceI.add(hostDo);
            rs = ResultBean.ok("保存成功！");
        }catch (Exception e){
            e.printStackTrace();
            rs = ResultBean.error("保存失败！");
        }
        return rs;
    }
    @RequestMapping("fixHost")
    @ResponseBody
    public ResultBean fixHost(@RequestBody HostDo hostDo, HttpServletRequest request){
        ResultBean rs = null;
        try {
            hostDo.setCreateTime(UtilTimeFormatter.getTimeToYMDHMS(new Date().getTime()));
            hostServiceI.update(hostDo);
            rs = ResultBean.ok("保存成功！");
        }catch (Exception e){
            e.printStackTrace();
            rs = ResultBean.error("保存失败！");
        }
        return rs;
    }
    @RequestMapping("getHostInfoList")
    @ResponseBody
    public ResultBean getHostInfoList(){
        ResultBean rs = null;
        try {
            List list = hostServiceI.queryAll();
            rs = ResultBean.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            rs = ResultBean.error("系统异常！");
        }
        return rs;
    }
    @RequestMapping("delHost")
    @ResponseBody
    public ResultBean delHost( HttpServletRequest request){
        ResultBean rs = null;
        try {
            String ids = request.getParameter("ids");
            hostServiceI.del(ids);
            rs = ResultBean.ok();
        }catch (Exception e){
            e.printStackTrace();
            rs = ResultBean.error("系统异常！");
        }
        return rs;
    }
}
